<script>
import Vue from 'vue'
import auth from 'common/js/auth.js'
import config from 'common/js/config.js'
import $Store from '@/store/index.js'
export default {
	mixins: [auth],
	onLaunch(data) {
		console.log('测试')
		uni.hideTabBar()
		if (uni.getStorageSync('baseUrl') != config.baseUrl) {
			uni.clearStorageSync()
		}
		uni.setStorageSync('baseUrl', config.baseUrl)

		// #ifdef MP
		const updateManager = uni.getUpdateManager()
		updateManager.onCheckForUpdate((res) => {
			// 请求完新版本信息的回调
		})

		updateManager.onUpdateReady((res) => {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate()
					}
				}
			})
		})

		updateManager.onUpdateFailed((res) => {
			// 新的版本下载失败
		})
		// #endif

		uni.getLocation({
			type: 'gcj02',
			success: res => {
				let obj = uni.getStorageSync('location')
				if (obj) {
					let a = this.$util.getDistance(obj.latitude, obj.longitude, res.latitude, res.longitude)
					if (a > 20) {
						uni.removeStorageSync('store')
					}
				}
				uni.setStorage({
					key: 'location',
					data: {
						latitude: res.latitude,
						longitude: res.longitude
					}
				})
			}
		})

		// 判断是否支持 获取本地位置
		// #ifdef H5
		if (navigator.geolocation) {
			var n = navigator.geolocation.getCurrentPosition((res) => {
				console.log(res) // 需要的坐标地址就在res中
			})
		} else {
			console.log('该浏览器不支持定位')
		}

		// #endif

		// #ifdef H5
		if (uni.getSystemInfoSync().platform == 'ios') {
			uni.setStorageSync('initUrl', location.href)
		}
		// #endif

		uni.onNetworkStatusChange((res) => {
			if (!res.isConnected) {
				uni.showModal({
					title: '网络失去链接',
					content: '请检查网络链接',
					showCancel: false
				})
			}
		})

		// console.log('App Launch', this.$RouteAll, '所有路由表，进来先校验登录');
		// 如果存在token把token存放起来并且拉去最新用户信息----目前内嵌到app的页面有问题
		uni.getSystemInfo({
			success(e) {
				// #ifndef MP
				Vue.prototype.StatusBar = e.statusBarHeight
				if (e.platform == 'android') {
					Vue.prototype.CustomBar = e.statusBarHeight + 50
				} else {
					Vue.prototype.CustomBar = e.statusBarHeight + 45
				}
				// #endif

				// #ifdef MP-WEIXIN
				Vue.prototype.StatusBar = e.statusBarHeight
				let custom = wx.getMenuButtonBoundingClientRect()
				Vue.prototype.Custom = custom
				Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight
				// #endif

				// #ifdef MP-ALIPAY
				Vue.prototype.StatusBar = e.statusBarHeight
				Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight
				// #endif
			}
		})

		// #ifdef MP-WEIXIN
		let newToken = uni.getStorageSync('token')
		if (newToken) {
			$Store.commit('SET_USER_TOKEN', newToken)
			this.$request.getUserInfo({ token: newToken }).then(res => {
				$Store.commit('SET_USER_INFO', res.data)
			})
		}
		// #endif

	},
	onShow() {

		this.$store.dispatch('init')

		// #ifdef H5
		if ($Store.getters.env.includes('NativeApp')) {
			window.login3rd = token => {
				console.log('ApponShow--login3rd', token)
				if (token) {
					$Store.commit('SET_USER_TOKEN', token)
					uni.setStorageSync('token', token)
					this.$request.getUserInfo({ token }).then(res => {
						uni.setStorageSync('userInfo', res.data)
						$Store.commit('SET_USER_INFO', res.data)
					})
				}
			}
		} else {
			let newToken = uni.getStorageSync('token')
			if (newToken) {
				$Store.commit('SET_USER_TOKEN', newToken)
				this.$request.getUserInfo({ token: newToken }).then(res => {
					uni.setStorageSync('userInfo', res.data)
					$Store.commit('SET_USER_INFO', res.data)
				})
			}
		}
		// #endif
		// 自动登录
		if (!uni.getStorageSync('token') && !uni.getStorageSync('loginLock') && !uni.getStorageSync('unbound')) {
			// #ifdef H5
			if (this.$util.isWeiXin()) {
				this.$util.getUrlCode(urlParams => {
					if (urlParams.suid) uni.setStorageSync('source_member', urlParams.suid)
					if (urlParams.code == undefined) {
						this.$api.sendRequest({
							url: '/wechat/api/wechat/authcode',
							data: {
								redirect_url: location.href
							},
							success: res => {
								if (res.code >= 0) {
									location.href = res.data
								}
							}
						})
					} else {
						this.$api.sendRequest({
							url: '/wechat/api/wechat/authcodetoopenid',
							data: {
								code: urlParams.code
							},
							success: res => {
								if (res.code >= 0) {
									let data = {}
									if (res.data.openid) data.wx_openid = res.data.openid
									if (res.data.unionid) data.wx_unionid = res.data.unionid
									if (res.data.userinfo) Object.assign(data, res.data.userinfo)
									this.authLogin(data)
								}
							}
						})
					}
				})
			}
			// #endif
		}
	},
	onHide() {
		this.$store.dispatch('getThemeStyle')
	},
	methods: {
		/**
         * 授权登录
         */
		authLogin(data) {
			uni.setStorage({
				key: 'authInfo',
				data
			})

			if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member')

			this.$api.sendRequest({
				url: '/api/login/auth',
				data,
				success: res => {
					if (res.code >= 0) {
						uni.setStorage({
							key: 'token',
							data: res.data.token,
							success: () => {
								this.$store.dispatch('getCartNumber')
								this.$store.commit('setToken', res.data.token)
							}
						})
					} else {
						uni.setStorage({
							key: 'unbound',
							data: 1,
							success: () => { }
						})
					}
				}
			})
		}
	}
}
</script>
<style lang="scss">
@import url("/common/css/iconfont.css");
@import "./common/css/main.scss";
@import "./common/css/common.css";
@import "./common/colorui/main.css";
@import "./common/colorui/icon.css";
@import "./common/colorui/animation.css";
</style>
